//
// Created by 16933 on 2024/1/13.
//

#ifndef BALANCE_BIKE_BSP_I2C_H
#define BALANCE_BIKE_BSP_I2C_H
#include "stm32f1xx_hal.h"
#include "stdint.h"
#include "gpio.h"
extern GPIO_InitTypeDef GPIO_InitStruct;
/*! I2C引脚宏定义 */
#define I2C_SCL_PORT    GPIOB
#define I2C_SCL_PIN     GPIO_PIN_6
#define I2C_SDA_PORT    GPIOB
#define I2C_SDA_PIN     GPIO_PIN_7

/*! I2C基本操作宏定义 */

//#define SDA_IN()          {  GPIO_InitStruct.Pin = I2C_SDA_PIN;\
//                             GPIO_InitStruct.Mode = GPIO_MODE_INPUT;\
//                             GPIO_InitStruct.Pull = GPIO_NOPULL;\
//                             GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;\
//                             HAL_GPIO_Init(I2C_SDA_PORT, &GPIO_InitStruct);}
//
//#define SDA_OUT()         {GPIO_InitStruct.Pin = I2C_SDA_PIN;\
//                             GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;\
//                             GPIO_InitStruct.Pull = GPIO_PULLUP;\
//                             GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;\
//                             HAL_GPIO_Init(I2C_SDA_PORT, &GPIO_InitStruct);}


//#define SCL_UP()          HAL_GPIO_WritePin(I2C_SCL_PORT,I2C_SCL_PIN,GPIO_PIN_SET)
//#define SCL_DOWN()        HAL_GPIO_WritePin(I2C_SCL_PORT,I2C_SCL_PIN,GPIO_PIN_RESET)
//#define SDA_UP()          HAL_GPIO_WritePin(I2C_SDA_PORT,I2C_SDA_PIN,GPIO_PIN_SET)
//#define SDA_DOWN()        HAL_GPIO_WritePin(I2C_SDA_PORT,I2C_SDA_PIN,GPIO_PIN_RESET)
//#define SDA_READ()        HAL_GPIO_ReadPin(I2C_SDA_PORT,I2C_SDA_PIN)



#define CL_UP(__I2C__)          HAL_GPIO_WritePin((__I2C__)->SCL_GPIO_PORT,(__I2C__)->SCL_GPIO_PIN,GPIO_PIN_SET)
#define CL_DOWN(__I2C__)        HAL_GPIO_WritePin((__I2C__)->SCL_GPIO_PORT,(__I2C__)->SCL_GPIO_PIN,GPIO_PIN_RESET)
#define DA_UP(__I2C__)          HAL_GPIO_WritePin((__I2C__)->SDA_GPIO_PORT,(__I2C__)->SDA_GPIO_PIN,GPIO_PIN_SET)
#define DA_DOWN(__I2C__)        HAL_GPIO_WritePin((__I2C__)->SDA_GPIO_PORT,(__I2C__)->SDA_GPIO_PIN,GPIO_PIN_RESET)
#define DA_READ(__I2C__)        HAL_GPIO_ReadPin((__I2C__)->SDA_GPIO_PORT,(__I2C__)->SDA_GPIO_PIN)
#define DELAY()           delay_us(1)

typedef struct I2C_t {
    GPIO_TypeDef * SCL_GPIO_PORT;
    GPIO_TypeDef * SDA_GPIO_PORT;
    uint16_t SCL_GPIO_PIN;
    uint16_t SDA_GPIO_PIN;
}I2C_t;


void delay_ms(uint32_t ms);
void delay_us(uint32_t us);
//void I2C_Start();
//void I2C_Stop();
//void I2C_Send_ACK();
//void I2C_Send_NACK();
//uint8_t I2C_Wait_ACK();
//void I2C_Send_Byte(uint8_t Byte);
//uint8_t I2C_Receive_Byte(uint8_t  ack);
//uint8_t I2C_Read_Bytes(uint8_t addr,uint8_t len,uint8_t *buf);
//uint8_t I2C_Write_Bytes(uint8_t addr,uint8_t len,uint8_t *buf);

void I2C_Start(I2C_t *i2c_device);
void I2C_Stop(I2C_t *i2c_device);
void I2C_Send_ACK(I2C_t *i2c_device);
void I2C_Send_NACK(I2C_t *i2c_device);
uint8_t I2C_Wait_ACK(I2C_t *i2c_device);
void I2C_Send_Byte(I2C_t *i2c_device , uint8_t Byte);
uint8_t I2C_Receive_Byte(I2C_t *i2c_device , uint8_t  ack);
uint8_t I2C_Write_Bytes(I2C_t *i2c_device , uint8_t addr,uint8_t len,uint8_t *buf);
uint8_t I2C_Read_Bytes(I2C_t *i2c_device , uint8_t addr,uint8_t len,uint8_t *buf);
#endif //BALANCE_BIKE_BSP_I2C_H
